//
//  SearchTableViewController.m
//  AoJi
//
//  Created by MinwenYi on 15-3-4.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//

#import "SearchTableViewController.h"
#import "RecTableViewCell.h"
#import "SchoolDireViewController.h"
@interface SearchTableViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    UIView *view2;
    UITableView *rectableview;
    NSMutableArray *recArray;
    UISearchBar *seachbar;
    
}
@end

@implementation SearchTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    seachbar=[[UISearchBar alloc]initWithFrame:CGRectMake(60, 30, Mainwidth-80, 30)];
    seachbar.backgroundColor=[UIColor clearColor];
    seachbar.delegate=self;
    seachbar.placeholder=@"请输入关键字";
    
    UIView *bgview=[[UIView alloc]initWithFrame:CGRectMake(60, 30, Mainwidth-80, 30)];
    [self.view addSubview:bgview];
    //    bgview.backgroundColor=kCorlor(226, 225, 225);
    bgview.backgroundColor=[UIColor yellowColor];
    
    //去掉搜索框背景
//    //1.
    UITextField *temview=[seachbar.subviews objectAtIndex:0];
    [temview addSubview:bgview];
    temview.backgroundColor=[UIColor redColor];
//    [seachbar addSubview:bgview];
    seachbar.tintColor=[UIColor redColor];
  
    [self.view addSubview:seachbar];
    
    [self dealview2];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealview2
{
    view2.backgroundColor=kcontentBgcolor;
    view2=[[UIView alloc]initWithFrame:CGRectMake(0, self.headerHeight, Mainwidth, MainHeight-self.headerHeight)];
    [self.view addSubview:view2];
    
    rectableview=[[UITableView alloc]initWithFrame:CGRectMake(0,0, Mainwidth, viewHeight(view2)) style:UITableViewStylePlain];
    rectableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    rectableview.showsVerticalScrollIndicator=NO;
    rectableview.backgroundColor=kcontentBgcolor;
    rectableview.dataSource=self;
    rectableview.delegate=self;
    [rectableview registerClass:[RecTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    [view2 addSubview:rectableview];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return recArray.count;
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.paramdic=recArray[indexPath.row];
    
    return cell;


}
-(void)recRequestTyep:(NSString*)searchStr
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    
    [dic setObject:self.catdir forKey:@"ccode"];
    [dic setObject:searchStr forKey:@"cschool"];
    ///cent.dsgrd.com/study/school?ccode=us&lastid=0
    
    [YHNetwork getInformationWithReguestType:RequestTypeGet WithParameters:dic WithApi:@"study/school" WhenSuccess:^(NSMutableDictionary *response) {
        recArray=response;
        [rectableview reloadData];
    } WhenFailure:^(NSError *failure) {
        
    }];
    
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==rectableview) {
        SchoolDireViewController *vC=[[SchoolDireViewController alloc]init];
        
        NSString *tem=[NSString stringWithFormat:@"http://cent.dsgrd.com/study/school/detail?id=%@",[recArray[indexPath.row] objectForKey:@"id"]];
        vC.urlString=tem;
        vC.titlel=[recArray[indexPath.row] objectForKey:@"cschool"];
        [self.navigationController pushViewController:vC animated:YES];
    }
    
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"search==%@",seachbar.text);
    if (seachbar.text.length>0) {
        [self recRequestTyep:seachbar.text];
    }
    
    [seachbar resignFirstResponder];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
